package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.Manufacturer;
import com.vne.skyemart.service.ManufacturerService;

@Controller
@SessionAttributes
@RequestMapping("/manufacturer")
public class ManufacturerController {

	@Autowired
	private ManufacturerService manufacturerService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveManufacturer(@ModelAttribute(" manufacturer") Manufacturer manufacturer,
			BindingResult result) {
		 manufacturerService.addManufacturer(manufacturer);
		return new ModelAndView("redirect:/manufacturer.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listManufacturer(Model model) {
		model.addAttribute("manufacturer",  manufacturerService.listManufacturer());

		return "manufacturerList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addManufacturer(@ModelAttribute("manufacturer") Manufacturer manufacturer,
			BindingResult result) {
		return new ModelAndView("addManufacturer");
	}

}
